package com.cmy.webCore.vo;

import com.cmy.webCore.enums.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author:Cmy
 * @Date:2022/11/21-16:50
 * @Description:
 */
@Data
@NoArgsConstructor
public class R<T> {

    /**
     * 状态码
     * @mock 200
     * @since v1.0.0
    */
    private Integer code;

    /**
     * 返回消息
     * @mock success
     * @since v1.0.0
    */
    private String message;

    /**
     * 状态
     * @mock true
     * @since v1.0.0
    */
    private boolean success;

    /**
     * 数据
    */
    private T data;

    public R(Integer code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public R(IResultCode resultCode, boolean success, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.success = success;
        this.data = data;
    }

    public R(IResultCode resultCode, boolean success) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.success = success;
        this.data = null;
    }

    public static <T> R success() {
        return new R<>(ResultCode.OK, true);
    }

    public static <T> R message(String message) {
        return new R<>(ResultCode.OK.getCode(), message, true, null);
    }

    public static <T> R success(T data) {
        return new R<>(ResultCode.OK, true, data);
    }

    public static <T> R fail() {
        return new R<>(ResultCode.ERROR, false);
    }

    public static <T> R fail(IResultCode resultCode) {
        return new R<>(resultCode, false);
    }

    public static <T> R fail(Integer code, String message) {
        return new R<>(code, message, false, null);
    }

    public static <T> R fail(IResultCode resultCode, T data) {
        return new R<>(resultCode, false, data);
    }

    public static <T> R fail(Integer code, String message, T data) {
        return new R<>(code, message, false, data);
    }

    public static <T> R check(Boolean success){
        if (success){
            return R.success();
        }
        return R.fail();
    }
}

    